<?php

class SF_Shippingquery_Helper_Data extends Mage_Core_Helper_Abstract
{
	protected $_host = "http://127.0.0.1:8080/rumall/services/rumallInterService?wsdl";

	public function returnShippingPrice($vendorId,$shippingCode, $weight='0')
	{
		try
		{
			$soap = new SoapClient($this->_host);
		}
		catch (Exception $e)
		{
			return false;
		}
		$result = $soap->getLogisticsPrice($weight,$vendorId);
		$xml=simplexml_load_string($result);
		if(!empty($xml))
		{
			switch ($shippingCode)
			{
				case '16':
					$shippingType = $xml->Body->Guahao;
					foreach ($shippingType->Price as $k => $value) 
					{
						if($value->CountryCode == 'RU')
						{
							$shippingPrice = (float)$value->Price;
							break;
						}
					}
					break;
				case '14':
					$shippingType = $xml->Body->Zhuandi;
					$shippingPrice = (float)$shippingType->Price->Price;
					break;
				case '0': //表示shippingCode为空，也返回price为0
					$shippingPrice = 0;
					break;
			}
			return $shippingPrice;
		}
		return 0;
		
	}


	//用于算每种运费之间的差价
	public function returnAllShippingPrice($vendorId,$weight='0')
	{
		try 
		{
			$soap = new SoapClient($this->_host);
		} 
		catch (Exception $e) 
		{
			return false;
		}
		
		$result = $soap->getLogisticsPrice($weight,$vendorId);
		$xml=simplexml_load_string($result);
		$shippingPrices = array();
		if(!empty($xml))
		{
			$shippingType = $xml->Body->Guahao;
			foreach ($shippingType->Price as $value) 
			{
				if($value->CountryCode == 'RU')
				{
					$shippingPrice = (float)$value->Price;
					$shippingPrices['Registered Mail'] = $shippingPrice;
					break;
				}
			}

			$shippingType = $xml->Body->Zhuandi;
			$shippingPrice = (float)$shippingType->Price->Price;
			$shippingPrices['Russia E-commerce Express'] = $shippingPrice;
			
			return $shippingPrices;
		}
		else
		{
			return 0;
		}

	}

}
